package com.mzpai.util.generate;

import java.lang.reflect.Field;

public class GenerateController<T> extends GenerateBase<T> {

  private static final String PACKAGE_TYPE = "controller";
  
  private static String service_name = "";
  private static String class_lows_name = "";

  public GenerateController(Class<T> clazz, String moduleName, String author) {
    super(clazz, PACKAGE_TYPE, moduleName, author); 
  }

  @Override
  protected void generaeImport() {
    addImport(CLAZZ_NAME, false);
    addImport("com.mzpai.product.service."+clazz.getSimpleName() + "Service", false);
    addImport("java.util.Map", false);
    addImport("javax.servlet.http.HttpServletRequest", false);
    addImport("javax.servlet.http.HttpServletResponse", false);
    addImport("org.springframework.stereotype.Controller", false);
    addImport("org.springframework.web.bind.annotation.RequestMapping", false);
    addImport("org.springframework.beans.factory.annotation.Autowired", false);
    addImport("com.github.pagehelper.PageInfo", false);
    addImport("org.springframework.web.bind.annotation.ResponseBody", false);
    addImport("org.slf4j.Logger", false);
    addImport("org.slf4j.LoggerFactory", false);
    addImport("com.mzpai.vo.ResponseVo", false);
    addImport("java.util.List", false);
    
  }
  
  @Override
  protected void generaeCalssTitle() {
    StringBuffer sb = new StringBuffer("@Controller");
    sb.append(NEXT_LINE);
    sb.append("@RequestMapping(\"/" + Character.toLowerCase(clazz.getSimpleName().charAt(0)) + clazz.getSimpleName().substring(1) + "\")");
    sb.append(NEXT_LINE);
    sb.append("public class ");
    sb.append(clazz.getSimpleName());
    sb.append(SUFFIX);
    /***
    sb.append(" extends BaseServiceImpl<");
    sb.append(clazz.getSimpleName());
    sb.append("> implements ");
    **/
    sb.append(" extends BaseController {");
    sb.append(NEXT_LINE);
    addClassLine(sb.toString());
  }

  @Override
  protected void generaeCalssContext() {
	  
	  	class_lows_name = Character.toLowerCase(clazz.getSimpleName().charAt(0)) + clazz.getSimpleName().substring(1);
	  	service_name = class_lows_name + "Service";
	  
	//  删除 
		StringBuffer sb = new StringBuffer("").append(NEXT_LINE);
		sb.append("	@Autowired ").append(NEXT_LINE);
		sb.append("	private ").append(clazz.getSimpleName()).append("Service ").append(service_name).append(";").append(NEXT_LINE);
		sb.append("	private static Logger log = LoggerFactory.getLogger(").append(clazz.getSimpleName()).append("Controller.class);").append(NEXT_LINE);
		
		sb.append("	/** ").append(NEXT_LINE);
		sb.append("	*").append(NEXT_LINE);
		sb.append("	* 查询列表 ").append(NEXT_LINE);
		sb.append("	* @throws Exception").append(NEXT_LINE);
		sb.append("	*/").append(NEXT_LINE);
		sb.append("	@RequestMapping(\"/getList\")").append(NEXT_LINE);
		sb.append("	@ResponseBody").append(NEXT_LINE);
		sb.append("	public ResponseVo<List<").append(clazz.getSimpleName()).append(">> getList(HttpServletRequest request, HttpServletResponse response) throws Exception {").append(NEXT_LINE);
		sb.append("		ResponseVo<List<").append(clazz.getSimpleName()).append(">> res=new ResponseVo<List<").append(clazz.getSimpleName()).append(">>();").append(NEXT_LINE);
		sb.append("		Map<String, Object> searchMap = searchMap(request);").append(NEXT_LINE);
		sb.append("		List<").append(clazz.getSimpleName()).append("> list = ").append(service_name).append(".getList(searchMap);").append(NEXT_LINE);
		sb.append("		res.setResult(list);").append(NEXT_LINE);
		sb.append("		return res;").append(NEXT_LINE);
		sb.append("	}").append(NEXT_LINE);
		sb.append(NEXT_LINE);
		sb.append(NEXT_LINE);
		

		 addClassLine(sb.toString());
		
  }

	private String getArr(Class<T> clazz) {
		StringBuffer sb = new StringBuffer(",");
		for (Field field : clazz.getDeclaredFields()) {
			sb.append("\"").append(field.getName()).append("\", ");
		}
		return sb.toString().substring(1);
	}
}
